<?php
/**
 * @title 1094. 拼车
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $trips
     * @param Integer $capacity
     * @return Boolean
     */
    function carPooling($trips, $capacity) {
        /**
         * @since 2023-12-02 每个位置，旅客上下车后，变化的人数
         */
        $locationChangePassengerArray = [];

        /**
         * @since 2023-12-02 遍历旅行数组
         */
        foreach ($trips as list($numPassengers, $from, $to)){
            /**
             * @since 2023-12-02 $form位置上车
             */
            if(isset($locationChangePassengerArray[$from])){
                $locationChangePassengerArray[$from] += $numPassengers;
            } else {
                $locationChangePassengerArray[$from] = $numPassengers;
            }

            /**
             * @since 2023-12-02 $to位置下车
             */
            if(isset($locationChangePassengerArray[$to])){
                $locationChangePassengerArray[$to] -= $numPassengers;
            } else {
                $locationChangePassengerArray[$to] = 0-$numPassengers;
            }
        }

        /**
         * @since 2023-12-02 按照里程排序
         */
        $locationArray = array_keys($locationChangePassengerArray);
        array_multisort($locationArray, SORT_ASC, SORT_NUMERIC, $locationChangePassengerArray);

        /**
         * @since 2023-12-02 计算每个位置的车上人数
         */
        $totalPassengers = 0;
        foreach ($locationChangePassengerArray as $changePassengers){
            $totalPassengers += $changePassengers;

            /**
             * @since 2023-12-02 判断当前人数是否超过车载最大人数
             */
            if($totalPassengers > $capacity){
                return false;
            } else {}
        }

        /**
         * @return
         */
        return true;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.02.php
 */
$datas = [
    [[[2,1,5],[3,3,7]], 4], // false
    [[[2,1,5],[3,3,7]], 5], // true
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->carPooling(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
